﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InventoryMIS2019
{
    public partial class _Category : InventoryMIS2019.Templates.BaselnfoForm
    {
        private CategoryBLL bl;//业务逻辑访问类对象
        private Category category;//业务实体类对象
        private List<Category> list;//建立泛型集合对象 
        public _Category()
        {
            InitializeComponent();
            bl = new CategoryBLL();
            BindDataGridView();
        }
      
        private void TemNew_Click(object sender, EventArgs e)
        {

        }
        private string Validation()
        {
            if (CatBox1leixingID.Text.Trim() == "")
            {
                CatBox1leixingID.Focus();//将光标定位值产品类型编号输入框
                return "产品编号不能为空！";
            }
            if (CatBox2leibiename.Text.Trim() == "")
            {
                CatBox2leibiename.Focus();//将光标定位值产品类型名称输入框
                return "产品名称不能为空！";
            }
            return "";
        }
        /// <summary>
        /// 表格数据绑定
        /// </summary>
        private void BindDataGridView()
        {
            dbcategory.DataSource = null;//清空当前表格中的数据
            list = bl.GetList();
            dbcategory.DataSource = list;//将查询结果绑定至控件dgvCategory
        }

        private void TemInventory_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0)
                MessageBox.Show(errorString, "系统提示");
            else
            {
                //如果操作值为INSERT，则进行新增操作
                if (opera == Operation.INSERT)
                {
                    string cID = CatBox1leixingID.Text.Trim();//获取控件中categoryID值
                    string cName = CatBox2leibiename.Text.Trim();//获取空间中categoryName值
                    category = new Category(cID, cName);//调用Category有参构造函数进行传值和实例化
                    if (bl.Insert(category))
                    {
                        BindDataGridView();
                    }

                }
                if (opera == Operation.UPDATE)
                {
                    string cID = CatBox1leixingID.Text.Trim();//获取控件中categoryID值
                    string cName = CatBox2leibiename.Text.Trim();//获取空间中categoryName值
                    category = new Category(cID, cName);//调用Category有参构造函数进行传值和实例化
                    if (bl.Update(category))
                    {
                        BindDataGridView();
                    }
                }
                if (opera == Operation.DELETE)
                {
                    string cID = CatBox1leixingID.Text.Trim();//获取控件中categoryID值
                    string cName = CatBox2leibiename.Text.Trim();//获取空间中categoryName值
                    category = new Category(cID, cName);//调用Category有参构造函数进行传值和实例化
                    if (bl.Delete(category))
                    {
                        BindDataGridView();
                    }
                }
            }
        }

        private void dbcategory_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string cID = dbcategory.CurrentRow.Cells["CategoryID"].Value.ToString();
            category = bl.FindByID(cID);
            if (opera == Operation.UPDATE || opera == Operation.DELETE)
            {
                CatBox1leixingID.Text = category.CategoryID;
                CatBox2leibiename.Text = category.CategoryName; 
            }
        }
    }
}
